package cn.iocoder.yudao.module.book.controller.admin.classes;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.classes.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.classes.ClassesDO;
import cn.iocoder.yudao.module.book.service.classes.ClassesService;

@Tag(name = "管理后台 - 班级")
@RestController
@RequestMapping("/book/classes")
@Validated
public class ClassesController {

    @Resource
    private ClassesService classesService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    @PreAuthorize("@ss.hasPermission('book:classes:create')")
    public CommonResult<Integer> createClasses(@Valid @RequestBody ClassesSaveReqVO createReqVO) {
        return success(classesService.createClasses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级")
    @PreAuthorize("@ss.hasPermission('book:classes:update')")
    public CommonResult<Boolean> updateClasses(@Valid @RequestBody ClassesSaveReqVO updateReqVO) {
        classesService.updateClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:classes:delete')")
    public CommonResult<Boolean> deleteClasses(@RequestParam("id") Integer id) {
        classesService.deleteClasses(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:classes:query')")
    public CommonResult<ClassesRespVO> getClasses(@RequestParam("id") Integer id) {
        ClassesDO classes = classesService.getClasses(id);
        return success(BeanUtils.toBean(classes, ClassesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级分页")
    @PreAuthorize("@ss.hasPermission('book:classes:query')")
    public CommonResult<PageResult<ClassesRespVO>> getClassesPage(@Valid ClassesPageReqVO pageReqVO) {
        PageResult<ClassesDO> pageResult = classesService.getClassesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级 Excel")
    @PreAuthorize("@ss.hasPermission('book:classes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassesExcel(@Valid ClassesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassesDO> list = classesService.getClassesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级.xls", "数据", ClassesRespVO.class,
                        BeanUtils.toBean(list, ClassesRespVO.class));
    }

}